package com.maxlen.goods.controller;

import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.R;
import com.maxlen.common.valid.AddValid;
import com.maxlen.common.valid.UpdateValid;
import com.maxlen.goods.entity.ProductAttrValueEntity;
import com.maxlen.goods.entity.dto.AttrInfoDTO;
import com.maxlen.goods.entity.dto.ProductAttrValueDTO;
import com.maxlen.goods.entity.vo.AttrVO;
import com.maxlen.goods.entity.vo.ProductAttrValueVO;
import com.maxlen.goods.service.AttrService;
import com.maxlen.goods.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author MaxLen
 * @email MaxLen@gmail.com
 * @date 2022-10-27 10:12:58
 */
@RestController
@RequestMapping("goods/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 查询指定 spu 的规格参数
     */
    @GetMapping("/base/listforspu/{spuId}")
    // @RequiresPermissions("goods:attrgroup:list")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.baseAttrListForSpu(spuId);
        List<ProductAttrValueDTO> productAttrValueDTOList = CopyUtils.copyList(productAttrValueEntityList, ProductAttrValueDTO.class);
        return R.ok().put("data", productAttrValueDTOList);
    }

    /**
     * 修改指定 spu 的规格参数
     */
    @PostMapping("/update/{spuId}")
    // @RequiresPermissions("goods:attr:update")
    public R updateAttrForSpu(@PathVariable("spuId") Long spuId, @Validated @RequestBody List<ProductAttrValueVO> productAttrValueVO){
        productAttrValueService.updateSpuAttr(spuId, productAttrValueVO);
        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    // @RequiresPermissions("goods:attrgroup:list")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable Long catelogId,
                          @PathVariable String attrType){
        PageUtils page = attrService.queryPage(params, catelogId, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    // @RequiresPermissions("goods:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    // @RequiresPermissions("goods:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrInfoDTO attrInfoDto = attrService.getDetailById(attrId);
        return R.ok().put("attr", attrInfoDto);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    // @RequiresPermissions("goods:attr:save")
    public R save(@Validated(AddValid.class) @RequestBody AttrVO attrVO){
		attrService.saveDetail(attrVO);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    // @RequiresPermissions("goods:attr:update")
    public R update(@Validated(UpdateValid.class) @RequestBody AttrVO attrVO){
		attrService.updateDetail(attrVO);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    // @RequiresPermissions("goods:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeDetail(Arrays.asList(attrIds));
        return R.ok();
    }

}
